export default {
    timeDesc:timeDesc,
    timeStr:timeStr,
    isFloat:isFloat,
    price:price
}

/** time可以是时间戳（秒单位）或者日期类型，
 * time如果是字符串，格式应该是： 2019-07-23T16:31:30+08:00 或者 2019-07-23 16:31:30 */
function timeStr (date, formate) {
    let year, month, day, hour, minute, second;
    let objType = Object.prototype.toString.call(date);
    if (objType === "[object Date]") {
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        hour = date.getHours();
        minute = date.getMinutes();
        second = date.getSeconds();
    }
    else if (objType === "[object Number]") {
        if (date <= 0) {return "-"}

        let d = new Date(parseInt(date) * 1000);
        if (d) {
            year = d.getFullYear();
            month = d.getMonth() + 1;
            day = d.getDate();
            hour = d.getHours();
            minute = d.getMinutes();
            second = d.getSeconds();
        }
    } else if (objType === "[object String]") {
        try {
            year = parseInt(date.substr(0,4));
            month = parseInt(date.substr(5,2));
            day = parseInt(date.substr(8,2));
            hour = parseInt(date.substr(11,2));
            minute = parseInt(date.substr(14,2));
            second = parseInt(date.substr(18,2));
        } catch (res) {
            return ""
        }
    }

    if (!year || year < 1900) {
        return "-"
    }

    if (month < 10) {month = '0' + month}
    if (day < 10) {day = '0' + day}
    if (hour < 10) {hour = '0' + hour}
    if (minute < 10) {minute = '0' + minute}
    if (second < 10) {second = '0' + second}

    if (!formate) {formate = 'Y-M-D H:M:S'}
    formate += '';
    switch (formate) {
        case 'Y-M-D H:M:S':
            return (year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ":" + second);
        case 'Y-M-D H:M':
            return (year + '-' + month + '-' + day + ' ' + hour + ':' + minute);
        case 'Y.M.D H:M:S':
            return (year + '.' + month + '.' + day + ' ' + hour + ':' + minute + ":" + second);
        case 'Y.M.D H:M':
            return (year + '.' + month + '.' + day + ' ' + hour + ':' + minute);
        case 'Y年M月D日 H:M':
            return (year + '年' + month + '月' + day + '日' + ' ' + hour + ':' + minute);
        case 'Y.M.D':
            return (year + '.' + month + '.' + day);
        case 'Y-M-D':
            return (year + '-' + month + '-' + day);
        case 'Y/M/D':
            return (year + '/' + month + '/' + day);
        case 'Y年M月D日':
            return (year + '年' + month + '月' + day + '日');
        case 'M月D日 H:M':
            return (month + '月' + day + '日' + " " + hour + ":" + minute);
        case 'M.D H:M':
            return (month + '.' + day + " " + hour + ":" + minute);
        case 'M月D日':
            return (month + '月' + day + '日');
        case 'M-D':
            return (month + '-' + day);
        case 'H:M':
            return (hour + ':' + minute);
        default:break;
    }
    return "";
}

/** time可以是时间戳（秒单位）或者日期类型，
 * time如果是字符串，格式应该是： 2019-07-23T16:31:30+08:00 或者 2019-07-23 16:31:30 */
function timeDesc (time) {
    let objType = Object.prototype.toString.call(time);
    let timeStamp = 0;
    if (objType === "[object Date]") {
        timeStamp = parseInt(time.getTime()/1000)
    }
    else if (objType === "[object String]") {
        try {
            let s = "";
            if (time.indexOf("T") > 0) {
                let ary = time.split("T");
                if (ary && ary.length > 0) {
                    s = ary[0];
        
                    let ary2 = ary[1].split('+');
                    if (ary2 && ary2.length >= 2) {
                        s +=  " " + ary2[0];
            
                        s = s.replace(/-/g,"/");
                        timeStamp = parseInt(new Date(s).getTime() / 1000);
                    }
                }
            }
            else {
                timeStamp = parseInt(new Date(time).getTime() / 1000);
            }

        } catch (err) {
            return ""
        }
    }
    else if (objType === "[object Number]") {

    }

    if (timeStamp === 0) {return ""}

    // 获取当前时间戳
    let currentTime = parseInt(new Date().getTime()/1000);
    let diffTime     = currentTime - timeStamp;

    if (diffTime <= 60) {
        if (diffTime <= 30) {
            return '刚刚';
        } else {
            return '1分钟前';
        }
    }
    else if (diffTime > 60 && diffTime < 60 * 60) {
        return parseInt((diffTime - 60) / 60.0) + 1 + "分钟前" ;
    }
    else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {
        return parseInt((diffTime - 3600) / 3600) + 1 + '小时前';
    }
    else {
        //超过1天
        return timeStr(time)
    }
}

/**
 * 是否为浮点数字
 * 范围：正整数、浮点正数（无符号）
 * @param {string | number} value 必选，被校验的值 (.11 这样的数字在校验时会自动转为0.11)
 * @param {number} length 可选，浮点位数，默认 2
 * */
function isFloat(value, length = 2) {
    return new RegExp(`^\\d+(?:\\.\\d{1,${length}})?$`).test(value)
}

/**
 * 转化为2位小数，四舍五入
 * @param {string | number} value 必选
 * */
function price(value) {
    if (typeof value === "number") {
        return value.toFixed(2)
    } else if (typeof value === 'string') {
        let v = parseFloat(value)
        return v.toFixed(2)
    }
    return "--"
}
